package com.party.admin.web.controller.copartner;

import com.party.admin.biz.copartner.CopartnerAnalyzeBizService;
import com.party.admin.biz.copartner.MemberCopartnerDetailBizService;
import com.party.admin.web.dto.AjaxResult;
import com.party.admin.web.dto.input.common.CommonInput;
import com.party.admin.web.dto.input.copartner.CopartnerAnalyzeInput;
import com.party.admin.web.dto.output.copartner.AnalyzeListOutput;
import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.model.YesNoStatus;
import com.party.core.model.activity.Activity;
import com.party.core.model.copartner.BrokerageItem;
import com.party.core.model.copartner.BrokerageListOutput;
import com.party.core.model.copartner.CopartnerLevelOutput;
import com.party.core.model.distributor.DistributorRelationType;
import com.party.core.model.distributor.WithCount;
import com.party.core.model.member.CopartnerAnalyze;
import com.party.core.model.member.CopartnerAnalyzeQuery;
import com.party.core.model.member.Member;
import com.party.core.service.copartner.IBrokerageItemService;
import com.party.core.service.copartner.ICopartnerLevelService;
import com.party.core.service.copartner.ITargetCopartnerService;
import com.party.core.service.distributor.IDistributorRelationService;
import com.party.core.service.member.ICopartnerAnalyzeService;
import com.party.core.service.member.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @Author: Administrator.
 * @Description: 合伙人众筹分析
 * @Date:Created in 2017/10/26 0026 下午 18:24.
 */
@Controller
@RequestMapping(value = "copartner/analyze")
public class CopartnerAnalyzeController {

    @Autowired
    private CopartnerAnalyzeBizService copartnerAnalyzeBizService;

    @Autowired
    private ICopartnerAnalyzeService copartnerAnalyzeService;

    @Autowired
    private ITargetCopartnerService targetCopartnerService;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private MemberCopartnerDetailBizService memberCopartnerDeatilBizService;

    @Autowired
    private ICopartnerLevelService copartnerLevelService;

    @Autowired
    private IBrokerageItemService brokerageItemService;

    @Autowired
    private IDistributorRelationService distributorRelationService;


    /**
     * 初始化数据
     *
     * @return 交互结果
     */
    @ResponseBody
    @RequestMapping(value = "initData")
    public AjaxResult initData(){
        memberCopartnerDeatilBizService.init();
        return AjaxResult.success();

    }


    /**
     * 列表
     *
     * @param analyzeQuery 查询条件
     * @param page         分页参数
     * @return 列表数据
     */
    @RequestMapping(value = "list")
    public ModelAndView list(CopartnerAnalyzeQuery analyzeQuery, Page page) {
        page.setLimit(20);
        ModelAndView mv = new ModelAndView("copartner/analyzeList");
        List<AnalyzeListOutput> list = memberCopartnerDeatilBizService.list(analyzeQuery, page);
        Map<String, Object> resultMap = copartnerAnalyzeBizService.getBrokerageData();
        mv.addObject("resultMap", resultMap);
        mv.addObject("list", list);
        mv.addObject("page", page);
        mv.addObject("analyzeQuery", analyzeQuery);
        return mv;
    }


    /**
     * 下级合伙人佣金详情列表
     * @param brokerageItem 查询条件
     * @param page    分页参数
     * @return 列表数据
     */
    @RequestMapping(value = "lowerCopartnerBrokerageDetailList")
    public ModelAndView lowerCopartnerBrokerageDetailList(BrokerageItem brokerageItem,
                                                          CopartnerAnalyzeQuery query, Page page) {
        page.setLimit(20);
        if (brokerageItem.getParentId() == null && brokerageItem.getPartnerId() == null) {
            return null;
/*            brokerageItem.setParentId("2e040619354c4c4580fdbcf110fe5a05");//顶级分销商
            brokerageItem.setPartnerId("ec9079a681194c82b9f2e77dc6877e1b");//上级分销商（合作商）*/
        }
        ModelAndView mv = new ModelAndView("copartner/lowerCopartBrokerageDetailList");
        List<BrokerageListOutput> list = brokerageItemService.adminlowerBrokerageDetailList(brokerageItem,query,page);
        mv.addObject("list", list);
        mv.addObject("page", page);
        mv.addObject("brokerageItem", brokerageItem);
        mv.addObject("query", query);
        Member member = memberService.get(brokerageItem.getPartnerId());
        mv.addObject("member", member);
        Member parent = memberService.get(brokerageItem.getParentId());
        mv.addObject("parent", parent);
        return mv;
    }


    /**
     * 初始化
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "init")
    public AjaxResult init() {
        // copartnerAnalyzeBizService.todayData();
        // copartnerAnalyzeBizService.copartnerActivityData();

        Member member = new Member();
        member.setIsCopartner(YesNoStatus.YES.getCode());
        List<Member> list = memberService.list(member);
        for (Member m : list) {
            CopartnerAnalyze merchantAnalyze = copartnerAnalyzeService.findByMemberId(m.getId());
            if (merchantAnalyze == null) {
                merchantAnalyze = new CopartnerAnalyze();
                merchantAnalyze.setApplyNum(0);
                merchantAnalyze.setOrderNum(0);
                merchantAnalyze.setSuccessNum(0);
                merchantAnalyze.setTotalMoney(0d);
                merchantAnalyze.setTotalBrokerage(0d);
                merchantAnalyze.setLogo(m.getLogo());
                merchantAnalyze.setName(m.getRealname());
                merchantAnalyze.setMobile(m.getMobile());
                merchantAnalyze.setRegisterDate(m.getCreateDate());
                merchantAnalyze.setMemberId(m.getId());
                copartnerAnalyzeService.insert(merchantAnalyze);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 数据分析详情
     *
     * @param page 分页参数
     * @return
     */
    @ResponseBody
    @RequestMapping("detail")
    public ModelAndView detail(String memberId, String title, CommonInput input, Page page) {
        Map<String, Object> params = CommonInput.appendParams(input);
        params.put("partnerId", memberId);
        params.put("title", title);
        params.put("orderBy", input.getOrderBy());
        page.setLimit(20);
        List<Map<String, Object>> resultMap = targetCopartnerService.webListPage(params, page);
        ModelAndView mv = new ModelAndView("copartner/analyzeDetail");

        Member partnerMember = memberService.get(memberId);
        mv.addObject("partnerMember", partnerMember);
        mv.addObject("list", resultMap);
        mv.addObject("page", page);
        mv.addObject("input", input);
        mv.addObject("memberId", memberId);
        mv.addObject("title", title);
        return mv;
    }


    /**
     * 合伙人数据分析--众筹项目分销
     * @param brokerageItem
     * @param input
     * @param page
     * @return
     */
    @RequestMapping("crowdfundDistributeList")
    public ModelAndView crowdfundDistributeList(BrokerageItem brokerageItem,CommonInput input, Page page) {
        Map<String, Object> params = CommonInput.appendParams(input);
        if (StringUtils.isBlank(brokerageItem.getParentId())){
            return null;
        }
        params.put("parentId", brokerageItem.getParentId());
        params.put("title", brokerageItem.getTitle());
        params.put("orderBy", input.getOrderBy());
        if(params.containsKey("c_start") && params.get("c_start")!=null){
            params.put("startDate",params.get("c_start"));
        }
        if(params.containsKey("c_end") && params.get("c_end")!=null){
            params.put("endDate",params.get("c_end"));
        }

        params.put("distributionType", DistributorRelationType.CROWDFUND_DISTRIBUTION.getCode());

        page.setLimit(20);

        List<WithCount> list =distributorRelationService.findCrowdfundDistriListByParentId(params,page);
        ModelAndView mv = new ModelAndView("copartner/crowdfundDistributeList");

        mv.addObject("list",list);
        mv.addObject("page",page);
        mv.addObject("params",params);
        mv.addObject("input",input);
        mv.addObject("memberId", brokerageItem.getParentId());
        mv.addObject("member",memberService.get(brokerageItem.getParentId()));
        mv.addObject("title", brokerageItem.getTitle());
        mv.addObject("isCopartner",0);

        return mv;
    }


    /**
     * 合伙人数据分析--课程分销
     * @param brokerageItem
     * @param input
     * @param page
     * @return
     */
    @RequestMapping("courseDistributeList")
    public ModelAndView courseDistributeList(BrokerageItem brokerageItem,CommonInput input, Page page, Integer paymentWay) {
        Map<String, Object> params = CommonInput.appendParams(input);

        if (StringUtils.isBlank(brokerageItem.getParentId())){
            return null;
        }
        params.put("parentId", brokerageItem.getParentId());
        params.put("title", brokerageItem.getTitle());
        params.put("orderBy", input.getOrderBy());
        if(params.containsKey("c_start") && params.get("c_start")!=null){
            params.put("startDate",params.get("c_start"));
        }
        if(params.containsKey("c_end") && params.get("c_end")!=null){
            params.put("endDate",params.get("c_end"));
        }
        params.put("paymentWay", paymentWay);

        //课程
        params.put("activityType",Activity.ACTIVITYTYPE_COURSE);
        params.put("distributionType", DistributorRelationType.COURSE_DISTRIBUTION.getCode());

        page.setLimit(20);

        List<WithCount> list =distributorRelationService.findCrowdfundDistriListByParentId(params,page);
        ModelAndView mv = new ModelAndView("copartner/courseDistributeList");

        mv.addObject("list",list);
        mv.addObject("page",page);
        mv.addObject("params",params);
        mv.addObject("input",input);
        mv.addObject("memberId", brokerageItem.getParentId());
        mv.addObject("member",memberService.get(brokerageItem.getParentId()));
        mv.addObject("title", brokerageItem.getTitle());
        mv.addObject("isCopartner",1);
        mv.addObject("paymentWay", paymentWay);
        return mv;
    }

    @RequestMapping("copartnerList")
    public ModelAndView copartnerList(CopartnerAnalyzeInput analyzeInput, CommonInput commonInput, Page page) {
        page.setLimit(10);
        if (null == analyzeInput.getSort()) {
            analyzeInput.setSort(1);
        }
        ModelAndView mv = new ModelAndView("/copartner/copartnerList");
        Map<String, Object> params = CommonInput.appendParams(commonInput);
        params.put("realname", analyzeInput.getRealname());
        params.put("mobile", analyzeInput.getMobile());
        params.put("sort", analyzeInput.getSort());
        params.put("parentId", analyzeInput.getParentId());

        List<CopartnerLevelOutput> levelOutputs = copartnerLevelService.copartnerList(params, page);
        mv.addObject("list", levelOutputs);
        mv.addObject("analyzeQuery", analyzeInput);
        mv.addObject("input", commonInput);
        mv.addObject("page", page);
        return mv;
    }
}
